home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / metkit / treedlg.cpp < prev    next >
C/C++ Source or Header  |  1997-06-07  |  6KB  |  205 lines

  1. //    Copyright (C) 1996, 1997 Meta Four Software.  All rights reserved.
  2. //
  3. //    Catalog tree display sample code
  4. //
  5. //! rev="$Id: treedlg.cpp,v 1.3 1997/05/27 00:06:25 jcw Rel $"
  6.  
  7. #include "stdafx.h"
  8. #include "catrecv.h"
  9. #include "TreeDlg.h"
  10.  
  11. #ifdef _DEBUG
  12. //#define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // These are the properties used in the directory catalog
  19.  
  20.     static c4_ViewProp        pDirs ("dirs"),
  21.                             pFiles ("files");
  22.     static c4_IntProp        pParent ("parent"),
  23.                             pSize ("size"),
  24.                             pDate ("date");
  25.     static c4_StringProp    pName ("name");
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CTreeDialog dialog
  29.  
  30. CTreeDialog::CTreeDialog (CFile& file_)
  31. {
  32.     _storage.LoadFromStream(&file_);    // load the data
  33.     _dirs = pDirs (_storage.Contents());
  34.  
  35.     //{{AFX_DATA_INIT(CTreeDialog)
  36.     //}}AFX_DATA_INIT
  37.  
  38.     Create(CTreeDialog::IDD);            // modeless stuff
  39. }
  40.  
  41. void CTreeDialog::OnCancel()
  42. {
  43.     DestroyWindow();                    // modeless stuff
  44. }
  45.  
  46. void CTreeDialog::PostNcDestroy()
  47. {
  48.     CDialog::PostNcDestroy();            // modeless stuff
  49.     delete this;
  50. }
  51.  
  52. void CTreeDialog::DoDataExchange(CDataExchange* pDX)
  53. {
  54.     CDialog::DoDataExchange(pDX);
  55.     //{{AFX_DATA_MAP(CTreeDialog)
  56.     DDX_Control(pDX, IDC_FILE_LIST, m_fileList);
  57.     DDX_Control(pDX, IDC_DIR_TREE, m_dirTree);
  58.     //}}AFX_DATA_MAP
  59. }
  60.  
  61. BEGIN_MESSAGE_MAP(CTreeDialog, CDialog)
  62.     //{{AFX_MSG_MAP(CTreeDialog)
  63.     ON_WM_SIZE()
  64.     ON_NOTIFY(TVN_SELCHANGED, IDC_DIR_TREE, OnSelchangedDirTree)
  65.     //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // [JCW,960119]  This code added for MetaKit CATRECV
  70.  
  71. BOOL CTreeDialog::OnInitDialog() 
  72. {
  73.     CDialog::OnInitDialog();
  74.  
  75.         // find out how wide the '0' character is in pixels
  76.     CDC* pDC = GetDC();
  77.     ASSERT(pDC);
  78.     int w = pDC->GetOutputTextExtent("0").cx;
  79.     ReleaseDC(pDC);
  80.  
  81.         // set up three columns with reasonable starting widths
  82.     VERIFY(m_fileList.InsertColumn(0, "Name", LVCFMT_LEFT, 12 * w) == 0);
  83.     VERIFY(m_fileList.InsertColumn(1, "Size", LVCFMT_RIGHT, 7 * w, 1) == 1);
  84.     VERIFY(m_fileList.InsertColumn(2, "Date", LVCFMT_LEFT, 6 * w, 2) == 2);
  85.   
  86.     RecalcLayout();
  87.  
  88.         // display the catalog contents
  89.     if (_dirs.GetSize() > 0)
  90.     {
  91.         CString path = pName (_dirs[0]);
  92.         SetWindowText("CATRECV - " + path);
  93.  
  94.         SetupDirTree();
  95.     }
  96.  
  97.     return TRUE;  // return TRUE unless you set the focus to a control
  98. }
  99.  
  100. // track changes in the main window size
  101. void CTreeDialog::OnSize(UINT nType, int cx, int cy) 
  102. {
  103.     CDialog::OnSize(nType, cx, cy);
  104.     
  105.     if (m_dirTree.m_hWnd && m_fileList.m_hWnd)
  106.         RecalcLayout();
  107. }
  108.  
  109. // share the client area between the tree control and the list control
  110. void CTreeDialog::RecalcLayout()
  111. {
  112.     CRect rect;
  113.     GetClientRect(&rect);
  114.     
  115.     ASSERT(rect.left == 0 && rect.top == 0);
  116.     int cx = rect.right;
  117.     int cy = rect.bottom;
  118.  
  119.     m_dirTree.MoveWindow(0, 0, cx/2, cy);
  120.     m_fileList.MoveWindow(cx/2, 0, cx - cx/2, cy); // careful with roundoff
  121. }
  122.  
  123. // Initialize the contents of the tree control with the directory structure
  124. // in the catalog that was just received. This version stores a copy of all
  125. // directory names in the tree control, could be optimized to use a callback
  126. // function to grab the name from the catalog whenever the control needs it.
  127. void CTreeDialog::SetupDirTree()
  128. {
  129.     HTREEITEM root, parent, item;
  130.  
  131.         // the root needs to be treated a little differently
  132.     root = m_dirTree.InsertItem(TVIF_TEXT | TVIF_PARAM, "(root)",
  133.                                         0, 0, 0, 0, 0, 0, TVI_SORT);
  134.  
  135.         // remember all tree control item handles, one per directory
  136.     _treeHandles.SetAtGrow(0, root);
  137.  
  138.         // now enter all remaining directories in the tree control
  139.     for (int i = 1; i < _dirs.GetSize(); ++i)
  140.     {
  141.         c4_RowRef thisDir = _dirs[i];
  142.  
  143.             // extract the relevant information from the catalog
  144.         CString dirName = pName (thisDir);
  145.         parent = (HTREEITEM) _treeHandles[pParent (thisDir)];
  146.  
  147.             // insert an item in the tree and remember its handle
  148.         item = m_dirTree.InsertItem(TVIF_TEXT | TVIF_PARAM, dirName,
  149.                                         0, 0, 0, 0, i, parent, TVI_SORT);
  150.         _treeHandles.SetAtGrow(i, item);
  151.     }
  152.  
  153.         // start off with a tree with an expanded root level
  154.     m_dirTree.Expand(root, TVE_EXPAND);
  155.  
  156.         // simulate a selection change (args are not really used here)
  157.     OnSelchangedDirTree(0, 0);
  158. }
  159.  
  160. // Update the file list when the directory selection changes. Again, a copy
  161. // of all strings is stored in the list control instead of using callbacks.
  162. void CTreeDialog::OnSelchangedDirTree(NMHDR* /* pNMHDR */, LRESULT* pResult) 
  163. {
  164.     VERIFY(m_fileList.DeleteAllItems());
  165.  
  166.     HTREEITEM item = m_dirTree.GetSelectedItem();
  167.     if (item)
  168.     {
  169.             // locate the corresponding list of files in the catalog
  170.         int dirIndex = m_dirTree.GetItemData(item);
  171.         c4_View files = pFiles (_dirs[dirIndex]);
  172.         
  173.         for (int i = 0; i < files.GetSize(); ++i)
  174.         {
  175.                 // extract the information from the catalog
  176.             CString name = pName (files[i]);
  177.             long size = pSize (files[i]);
  178.             long date = pDate (files[i]);
  179.  
  180.                 // create the main file entry
  181.             int n = m_fileList.InsertItem(i, name);
  182.             VERIFY(n >= 0);
  183.  
  184.             char buf [15];
  185.  
  186.                 // set subitem 1 to the file size
  187.             wsprintf(buf, "%ld", size);
  188.             m_fileList.SetItemText(n, 1, buf);
  189.  
  190.                 // set subitem 2 to the file date
  191.             if (date)
  192.             {
  193.                 wsprintf(buf, "%02d%02d%02d", 80 + (date >> 9),
  194.                                         (date >> 5) & 0x0F, date & 0x1F);
  195.                 m_fileList.SetItemText(n, 2, buf);
  196.             }
  197.         }
  198.     }
  199.     
  200.     if (pResult) // careful, this is null when called from SetupDirTree
  201.         *pResult = 0;
  202. }
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205.